
#include "stdafx.h"
#include "IPCCallPacket.h"

SerializeString::SerializeString()
{

}

SerializeString::SerializeString( const std::vector<asyframe::byte>& _array )
{
	R0(setValue(_array));
}

SerializeString::SerializeString( const char* _cstr )
{
	R0(setValue(_cstr));
}

SerializeString::SerializeString( const std::string& _str )
{
	R0(setValue(_str));
}

SerializeString::~SerializeString()
{

}

retT SerializeString::setValue( const std::vector<asyframe::byte>& _array )
{
	array_.clear();
	array_.resize(_array.size());
	std::copy(_array.begin(), _array.end(), array_.begin());
	RETSUCCESS;
}

retT SerializeString::setValue( const char* _cstr )
{
	array_.clear();
	array_.resize(strlen(_cstr));
	for(std::size_t i = 0; i < array_.size(); ++i)
		array_[i] = (asyframe::byte)_cstr[i];
	RETSUCCESS;
}

retT SerializeString::setValue( const std::string& _str )
{
	array_.clear();
	array_.resize(_str.length());
	for(std::size_t i = 0; i < array_.size(); ++i)
		array_[i] = (asyframe::byte)_str[i];
	RETSUCCESS;
}

std::string SerializeString::toString()
{
	if(array_.size() > 0)
		return std::string((char*)&array_[0], array_.size());
	return std::string();
}

/////////////////////////////////////////////////////////////////////////////////////////////

IPCCallPacketBase::IPCCallPacketBase()
{

}

IPCCallPacketBase::~IPCCallPacketBase()
{

}

retT IPCCallPacketBase::readFromBufferNoCheck( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	R(messageQueueName_.readFromBuffer(_pBuffer));
	R(overlayInstanceName_.readFromBuffer(_pBuffer));
	R(resourceName_.readFromBuffer(_pBuffer));
	RETSUCCESS;
}

retT IPCCallPacketBase::writeToBufferNoCheck( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	R(messageQueueName_.writeToBuffer(_pBuffer));
	R(overlayInstanceName_.writeToBuffer(_pBuffer));
	R(resourceName_.writeToBuffer(_pBuffer));
	RETSUCCESS;
}

retT IPCCallPacketBase::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	READFROMBUFFER_END;
}

retT IPCCallPacketBase::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t IPCCallPacketBase::size()
{
	return messageQueueName_.size() + overlayInstanceName_.size() + resourceName_.size();
}

retT IPCCallPacketBase::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
{
	R(PacketElement::attachRootElement(_pRootElement));
	R(messageQueueName_.attachRootElement(_pRootElement));
	R(overlayInstanceName_.attachRootElement(_pRootElement));
	R(resourceName_.attachRootElement(_pRootElement));
	RETSUCCESS;
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendStoreReqIPCCallPacket::SendStoreReqIPCCallPacket()
:pReloadPacket_()
{

}

SendStoreReqIPCCallPacket::~SendStoreReqIPCCallPacket()
{
	pReloadPacket_.reset();
}

retT SendStoreReqIPCCallPacket::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	if(!pReloadPacket_)
	{
		pReloadPacket_.reset(new reload::ReloadPacket());
		R(pReloadPacket_->attachRootElement());
	}
	pReloadPacket_->forwardingHeader_.overlay_ = reload::SHA1HashValue(overlayInstanceName_.toString().c_str());
	boost::shared_ptr<reload::StoreReq> pStoreReq(new reload::StoreReq());
	R(pStoreReq->attachRootElement(pReloadPacket_));
	pReloadPacket_->msgContents_.messageBody_.elements_.push_back(pStoreReq);
	R(pStoreReq->kindData_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendStoreReqIPCCallPacket::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	if(pReloadPacket_ && pReloadPacket_->msgContents_.messageBody_.elements_.size() > 0)
	{
		boost::shared_ptr<reload::StoreReq> pStoreReq = boost::dynamic_pointer_cast<reload::StoreReq>(pReloadPacket_->msgContents_.messageBody_.elements_[0]);
		if(pStoreReq)
		{
			R(pStoreReq->kindData_.writeToBuffer(_pBuffer));
		}
		else
			RET(ERROR_COMMON_SHARED_PTR_NULL);
	}
	WRITETOBUFFER_END;
}

std::size_t SendStoreReqIPCCallPacket::size()
{
	std::size_t totalSize = IPCCallPacketBase::size();
	if(pReloadPacket_ && pReloadPacket_->msgContents_.messageBody_.elements_.size() > 0)
	{
		boost::shared_ptr<reload::StoreReq> pStoreReq = boost::dynamic_pointer_cast<reload::StoreReq>(pReloadPacket_->msgContents_.messageBody_.elements_[0]);
		if(pStoreReq)
		{
			totalSize += pStoreReq->kindData_.size();
		}
	}
	return totalSize;
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendFetchReqIPCCallPacket::SendFetchReqIPCCallPacket()
:pReloadPacket_()
{

}

SendFetchReqIPCCallPacket::~SendFetchReqIPCCallPacket()
{
	pReloadPacket_.reset();
}

retT SendFetchReqIPCCallPacket::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	if(!pReloadPacket_)
	{
		pReloadPacket_.reset(new reload::ReloadPacket());
		R(pReloadPacket_->attachRootElement());
	}
	pReloadPacket_->forwardingHeader_.overlay_ = reload::SHA1HashValue(overlayInstanceName_.toString().c_str());
	boost::shared_ptr<reload::FetchReq> pFetchReq(new reload::FetchReq());
	R(pFetchReq->attachRootElement(pReloadPacket_));
	pReloadPacket_->msgContents_.messageBody_.elements_.push_back(pFetchReq);
	R(pFetchReq->specifiers_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendFetchReqIPCCallPacket::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	if(pReloadPacket_ && pReloadPacket_->msgContents_.messageBody_.elements_.size() > 0)
	{
		boost::shared_ptr<reload::FetchReq> pFetchReq = boost::dynamic_pointer_cast<reload::FetchReq>(pReloadPacket_->msgContents_.messageBody_.elements_[0]);
		if(pFetchReq)
		{
			R(pFetchReq->specifiers_.writeToBuffer(_pBuffer));
		}
		else
			RET(ERROR_COMMON_SHARED_PTR_NULL);
	}
	WRITETOBUFFER_END;
}

std::size_t SendFetchReqIPCCallPacket::size()
{
	std::size_t totalSize = IPCCallPacketBase::size();
	if(pReloadPacket_ && pReloadPacket_->msgContents_.messageBody_.elements_.size() > 0)
	{
		boost::shared_ptr<reload::FetchReq> pFetchReq = boost::dynamic_pointer_cast<reload::FetchReq>(pReloadPacket_->msgContents_.messageBody_.elements_[0]);
		if(pFetchReq)
		{
			totalSize += pFetchReq->specifiers_.size();
		}
	}
	return totalSize;
}

/////////////////////////////////////////////////////////////////////////////////////////////

IPCCallResultBase::IPCCallResultBase()
:result_(0)
{

}

IPCCallResultBase::~IPCCallResultBase()
{
	result_ = 0;
}

retT IPCCallResultBase::readFromBufferNoCheck( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	R(_pBuffer->ntohRead(result_));
	RETSUCCESS;
}

retT IPCCallResultBase::writeToBufferNoCheck( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	R(_pBuffer->htonWrite(result_));
	RETSUCCESS;
}

retT IPCCallResultBase::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	READFROMBUFFER_END;
}

retT IPCCallResultBase::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t IPCCallResultBase::size()
{
	return sizeof(result_);
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendFetchReqIPCCallResult::SendFetchReqIPCCallResult()
:pReloadPacket_()
{

}

SendFetchReqIPCCallResult::~SendFetchReqIPCCallResult()
{
	pReloadPacket_.reset();
}

retT SendFetchReqIPCCallResult::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	READFROMBUFFER_END;
}

retT SendFetchReqIPCCallResult::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	if(pReloadPacket_ && pReloadPacket_->msgContents_.messageBody_.elements_.size() > 0)
	{
		boost::shared_ptr<reload::FetchAns> pFetchAns = boost::dynamic_pointer_cast<reload::FetchAns>(pReloadPacket_->msgContents_.messageBody_.elements_[0]);
		if(pFetchAns)
		{
			R(pFetchAns->kindResponses_.writeToBuffer(_pBuffer));
		}
		else
			RET(ERROR_COMMON_SHARED_PTR_NULL);
	}
	WRITETOBUFFER_END;
}

std::size_t SendFetchReqIPCCallResult::size()
{
	std::size_t totalSize = IPCCallResultBase::size();
	if(pReloadPacket_ && pReloadPacket_->msgContents_.messageBody_.elements_.size() > 0)
	{
		boost::shared_ptr<reload::FetchAns> pFetchAns = boost::dynamic_pointer_cast<reload::FetchAns>(pReloadPacket_->msgContents_.messageBody_.elements_[0]);
		if(pFetchAns)
		{
			totalSize += pFetchAns->kindResponses_.size();
		}
	}
	return totalSize;
}

/////////////////////////////////////////////////////////////////////////////////////////////

SendAppAttachReqIPCCallResult::SendAppAttachReqIPCCallResult()
{

}

SendAppAttachReqIPCCallResult::~SendAppAttachReqIPCCallResult()
{

}

retT SendAppAttachReqIPCCallResult::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	READFROMBUFFER_START;
	R(readFromBufferNoCheck(_pBuffer));
	R(strIP_.readFromBuffer(_pBuffer));
	READFROMBUFFER_END;
}

retT SendAppAttachReqIPCCallResult::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
{
	WRITETOBUFFER_START;
	R(writeToBufferNoCheck(_pBuffer));
	R(strIP_.writeToBuffer(_pBuffer));
	WRITETOBUFFER_END;
}

std::size_t SendAppAttachReqIPCCallResult::size()
{
	return IPCCallResultBase::size() + strIP_.size();
}

retT SendAppAttachReqIPCCallResult::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
{
	R(asyframe::PacketElement::attachRootElement(_pRootElement));
	R(strIP_.attachRootElement(_pRootElement));
	RETSUCCESS;
}
